class UserModel {
  late String? createBy;
  late String? createTime;
  late String? updateBy;
  late String? updateTime;
  late String? remark;
  late int? userId;
  late int? deptId;
  late String? userName;
  late String? nickName;
  late String? email;
  late String? phonenumber;
  late String? userSwitch;
  late String? sex;
  late String? avatar;
  late String? password;
  late String? status;
  late String? delFlag;
  late String? loginIp;
  late String? loginDate;
  late Dept? dept;
  late List<Role>? roles;
  late List<int>? roleIds;
  late List<int>? postIds;
  late int? roleId;
  late bool? admin;

  UserModel({
    this.createBy,
    this.createTime,
    this.updateBy,
    this.updateTime,
    this.remark,
    this.userId,
    this.deptId,
    this.userName,
    this.nickName,
    this.email,
    this.phonenumber,
    this.userSwitch,
    this.sex,
    this.avatar,
    this.password,
    this.status,
    this.delFlag,
    this.loginIp,
    this.loginDate,
    this.dept,
    this.roles,
    this.roleIds,
    this.postIds,
    this.roleId,
    this.admin,
  });

  // 从JSON数据创建User实例的工厂方法
  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      createBy: json['createBy'] as String?,
      createTime: json['createTime'] as String?,
      updateBy: json['updateBy'] as String?,
      updateTime: json['updateTime'] as String?,
      remark: json['remark'] as String?,
      userId: json['userId'] as int?,
      deptId: json['deptId'] as int?,
      userName: json['userName'] as String?,
      nickName: json['nickName'] as String?,
      email: json['email'] as String?,
      phonenumber: json['phonenumber'] as String?,
      userSwitch: json['userSwitch'] as String?,
      sex: json['sex'] as String?,
      avatar: json['avatar'] as String?,
      password: json['password'] as String?,
      status: json['status'] as String?,
      delFlag: json['delFlag'] as String?,
      loginIp: json['loginIp'] as String?,
      loginDate: json['loginDate'] as String?,
      dept: json['dept'] != null ? Dept.fromJson(json['dept']) : null,
      roles: json['roles'] != null
          ? (json['roles'] as List<dynamic>)
          .map((e) => Role.fromJson(e))
          .toList()
          : null,
      roleIds: json['roleIds'] != null
          ? (json['roleIds'] as List<dynamic>).cast<int>()
          : null,
      postIds: json['postIds'] != null
          ? (json['postIds'] as List<dynamic>).cast<int>()
          : null,
      roleId: json['roleId'] as int?,
      admin: json['admin'] as bool?,
    );
  }

  // 将User实例转换为JSON格式
  Map<String, dynamic> toJson() {
    return {
      'createBy': createBy,
      'createTime': createTime,
      'updateBy': updateBy,
      'updateTime': updateTime,
      'remark': remark,
      'userId': userId,
      'deptId': deptId,
      'userName': userName,
      'nickName': nickName,
      'email': email,
      'phonenumber': phonenumber,
      'userSwitch': userSwitch,
      'sex': sex,
      'avatar': avatar,
      'password': password,
      'status': status,
      'delFlag': delFlag,
      'loginIp': loginIp,
      'loginDate': loginDate,
      'dept': dept?.toJson(),
      'roles': roles?.map((e) => e.toJson()).toList(),
      'roleIds': roleIds,
      'postIds': postIds,
      'roleId': roleId,
      'admin': admin,
    };
  }

  @override
  String toString() {
    return 'UserModel{'
        'userId: $userId, '
        'userName: $userName, '
        'nickName: $nickName, '
        'avatar: $avatar'
        '}';
  }
}

class Dept {
  late String? createBy;
  late String? createTime;
  late String? updateBy;
  late String? updateTime;
  late String? remark;
  late int? deptId;
  late int? parentId;
  late String? ancestors;
  late String? deptName;
  late int? orderNum;
  late String? leader;
  late String? phone;
  late String? email;
  late String? status;
  late String? delFlag;
  late String? parentName;
  late List<dynamic>? children;

  Dept({
    this.createBy,
    this.createTime,
    this.updateBy,
    this.updateTime,
    this.remark,
    this.deptId,
    this.parentId,
    this.ancestors,
    this.deptName,
    this.orderNum,
    this.leader,
    this.phone,
    this.email,
    this.status,
    this.delFlag,
    this.parentName,
    this.children,
  });

  factory Dept.fromJson(Map<String, dynamic> json) {
    return Dept(
      createBy: json['createBy'] as String?,
      createTime: json['createTime'] as String?,
      updateBy: json['updateBy'] as String?,
      updateTime: json['updateTime'] as String?,
      remark: json['remark'] as String?,
      deptId: json['deptId'] as int?,
      parentId: json['parentId'] as int?,
      ancestors: json['ancestors'] as String?,
      deptName: json['deptName'] as String?,
      orderNum: json['orderNum'] as int?,
      leader: json['leader'] as String?,
      phone: json['phone'] as String?,
      email: json['email'] as String?,
      status: json['status'] as String?,
      delFlag: json['delFlag'] as String?,
      parentName: json['parentName'] as String?,
      children: json['children'] as List<dynamic>?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'createBy': createBy,
      'createTime': createTime,
      'updateBy': updateBy,
      'updateTime': updateTime,
      'remark': remark,
      'deptId': deptId,
      'parentId': parentId,
      'ancestors': ancestors,
      'deptName': deptName,
      'orderNum': orderNum,
      'leader': leader,
      'phone': phone,
      'email': email,
      'status': status,
      'delFlag': delFlag,
      'parentName': parentName,
      'children': children,
    };
  }
}

class Role {
  late String? createBy;
  late String? createTime;
  late String? updateBy;
  late String? updateTime;
  late String? remark;
  late int? roleId;
  late String? roleName;
  late String? roleKey;
  late int? roleSort;
  late String? dataScope;
  late bool? menuCheckStrictly;
  late bool? deptCheckStrictly;
  late String? status;
  late String? delFlag;
  late bool? flag;
  late List<int>? menuIds;
  late List<int>? deptIds;
  late List<String>? permissions;
  late bool? admin;

  Role({
    this.createBy,
    this.createTime,
    this.updateBy,
    this.updateTime,
    this.remark,
    this.roleId,
    this.roleName,
    this.roleKey,
    this.roleSort,
    this.dataScope,
    this.menuCheckStrictly,
    this.deptCheckStrictly,
    this.status,
    this.delFlag,
    this.flag,
    this.menuIds,
    this.deptIds,
    this.permissions,
    this.admin,
  });

  factory Role.fromJson(Map<String, dynamic> json) {
    return Role(
      createBy: json['createBy'] as String?,
      createTime: json['createTime'] as String?,
      updateBy: json['updateBy'] as String?,
      updateTime: json['updateTime'] as String?,
      remark: json['remark'] as String?,
      roleId: json['roleId'] as int?,
      roleName: json['roleName'] as String?,
      roleKey: json['roleKey'] as String?,
      roleSort: json['roleSort'] as int?,
      dataScope: json['dataScope'] as String?,
      menuCheckStrictly: json['menuCheckStrictly'] as bool?,
      deptCheckStrictly: json['deptCheckStrictly'] as bool?,
      status: json['status'] as String?,
      delFlag: json['delFlag'] as String?,
      flag: json['flag'] as bool?,
      menuIds: json['menuIds'] != null
          ? (json['menuIds'] as List<dynamic>).cast<int>()
          : null,
      deptIds: json['deptIds'] != null
          ? (json['deptIds'] as List<dynamic>).cast<int>()
          : null,
      permissions: json['permissions'] != null
          ? (json['permissions'] as List<dynamic>).cast<String>()
          : null,
      admin: json['admin'] as bool?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'createBy': createBy,
      'createTime': createTime,
      'updateBy': updateBy,
      'updateTime': updateTime,
      'remark': remark,
      'roleId': roleId,
      'roleName': roleName,
      'roleKey': roleKey,
      'roleSort': roleSort,
      'dataScope': dataScope,
      'menuCheckStrictly': menuCheckStrictly,
      'deptCheckStrictly': deptCheckStrictly,
      'status': status,
      'delFlag': delFlag,
      'flag': flag,
      'menuIds': menuIds,
      'deptIds': deptIds,
      'permissions': permissions,
      'admin': admin,
    };
  }
}